﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mail;

namespace Project.Engine.Utilities
{
    public class MailUtil
    {
        public static bool CheckValidMail(string _mail)
        {
            if (String.IsNullOrEmpty(_mail) == true)
            {
                return false;
            }

            // return
            return Regex.IsMatch(_mail, "^([a-z._0-9])*@([a-z0-9]{1,50}).(com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|vn|)$");
        }

        public static bool SendMail(string to, string subject, string body) {
            try
            {
                DefineUtil define = new DefineUtil();
                string emailaddress = GetMailAddress(define.Email);
                string emailpassword = GetMailPass(define.Email);

                System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport","465");
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //Use 0 for anonymous
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", emailaddress);
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", emailpassword);
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                myMail.From = "Project - Reset password <" + emailaddress + ">";
                myMail.To = to;
                myMail.Subject = subject;
                myMail.BodyFormat = MailFormat.Html;
                myMail.BodyEncoding = System.Text.Encoding.UTF8;
                myMail.Body = body;

                System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
                System.Web.Mail.SmtpMail.Send(myMail);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static string GetMailAddress(string mail) {
            string [] s = mail.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length >= 1) {
                return s[0].ToString();
            }

            return String.Empty;
        }

        public static string GetMailPass(string mail)
        {
            string[] s = mail.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length >= 1)
            {
                return s[1].ToString();
            }

            return String.Empty;
        }
    }
}
